package com.gofaraway.service.juc;

import java.util.concurrent.CountDownLatch;

/**
 * 计数器
 * CountDownLatch
 *
 * @author ChenPeng
 * @Date 2024/3/22 10:04
 */
public class Demo2 {

    /*
        不使用计数器结果
        main继续向下执行
        Thread-2-----start
        Thread-4-----start
        Thread-3-----start
        Thread-0-----start
        Thread-5-----start
        Thread-1-----start

        使用计数器结果
        Thread-0-----start
        Thread-3-----start
        Thread-4-----start
        Thread-2-----start
        Thread-1-----start
        Thread-5-----start
        main继续向下执行
     */
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(5);

        for (int i = 0; i < 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "-----start");
                countDownLatch.countDown();
            }).start();
        }

        countDownLatch.await();

        System.out.println("main继续向下执行");
    }
}
